import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import home from '../components/home'
import Welcome from '../components/Welcome'
import users from '../components/user/users'
import rights from '../components/power/rights'
import Roles from '../components/power/Roles'
import goods from '../components/goods/goods'
import categories from '../components/goods/categories'
import params from '../components/goods/params'
import add from '../components/goods/add'
import orders from '../components/order/orders'
import report from '../components/reports/report'
/* 注册 */
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: users },
        { path: '/rights', component: rights },
        { path: '/Roles', component: Roles },
        { path: '/goods', component: goods },
        { path: '/categories', component: categories },
        { path: '/params', component: params },
        { path: '/goods/add', component: add },
        { path: '/orders', component: orders },
        { path: '/reports', component: report }]
    }
  ]
})
/* 挂载路由守卫 */
router.beforeEach((to, from, next) => {
  /* 从登录页来  放行 */
  if (to.path === '/login') return next()
  /* 否则 获取sessionStorage中token */
  const tokenStr = window.sessionStorage.getItem('token')
  /* 判断token 有值否  没有 token ,强制跳转到登录页面 */
  if (!tokenStr) return next('/login')
  next()
})

export default router
